3 * Copyright © 2011 Sam Reed
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
24 use MediaWiki\Revision\RevisionAccessException
;
25 use MediaWiki\Revision\RevisionRecord
;
26 use MediaWiki\Revision\RevisionStore
;
27 use MediaWiki\Revision\SlotRecord
;
32 class ApiFeedContributions
extends ApiBase
{
34 /** @var RevisionStore */
35 private $revisionStore;
37 /** @var TitleParser */
41 * This module uses a custom feed wrapper printer.
43 * @return ApiFormatFeedWrapper
45 public function getCustomPrinter() {
46 return new ApiFormatFeedWrapper( $this->getMain() );
49 public function execute() {
50 $this->revisionStore
= MediaWikiServices
::getInstance()->getRevisionStore();
51 $this->titleParser
= MediaWikiServices
::getInstance()->getTitleParser();
53 $params = $this->extractRequestParams();
55 $config = $this->getConfig();
56 if ( !$config->get( 'Feed' ) ) {
57 $this->dieWithError( 'feed-unavailable' );
60 $feedClasses = $config->get( 'FeedClasses' );
61 if ( !isset( $feedClasses[$params['feedformat']] ) ) {
62 $this->dieWithError( 'feed-invalid' );
65 if ( $params['showsizediff'] && $this->getConfig()->get( 'MiserMode' ) ) {
66 $this->dieWithError( 'apierror-sizediffdisabled' );
69 $msg = wfMessage( 'Contributions' )->inContentLanguage()->text();
70 $feedTitle = $config->get( 'Sitename' ) . ' - ' . $msg .
71 ' [' . $config->get( 'LanguageCode' ) . ']';
72 $feedUrl = SpecialPage
::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
75 if ( $params['user'] != 'newbies' ) {
77 $target = $this->titleParser
78 ->parseTitle( $params['user'], NS_USER
)
80 } catch ( MalformedTitleException
$e ) {
82 [ 'apierror-baduser', 'user', wfEscapeWikiText( $params['user'] ) ],
83 'baduser_' . $this->encodeParamName( 'user' )
88 $feed = new $feedClasses[$params['feedformat']] (
90 htmlspecialchars( $msg ),
94 // Convert year/month parameters to end parameter
95 $params['start'] = '';
97 $params = ContribsPager
::processDateFilter( $params );
99 $pager = new ContribsPager( $this->getContext(), [
101 'namespace' => $params['namespace'],
102 'start' => $params['start'],
103 'end' => $params['end'],
104 'tagFilter' => $params['tagfilter'],
105 'deletedOnly' => $params['deletedonly'],
106 'topOnly' => $params['toponly'],
107 'newOnly' => $params['newonly'],
108 'hideMinor' => $params['hideminor'],
109 'showSizeDiff' => $params['showsizediff'],
112 $feedLimit = $this->getConfig()->get( 'FeedLimit' );
113 if ( $pager->getLimit() > $feedLimit ) {
114 $pager->setLimit( $feedLimit );
118 if ( $pager->getNumRows() > 0 ) {
120 $limit = $pager->getLimit();
121 foreach ( $pager->mResult
as $row ) {
122 // ContribsPager selects one more row for navigation, skip that row
123 if ( ++
$count > $limit ) {
126 $item = $this->feedItem( $row );
127 if ( $item !== null ) {
128 $feedItems[] = $item;
133 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
136 protected function feedItem( $row ) {
137 // This hook is the api contributions equivalent to the
138 // ContributionsLineEnding hook. Hook implementers may cancel
139 // the hook to signal the user is not allowed to read this item.
141 $hookResult = Hooks
::run(
142 'ApiFeedContributions::feedItem',
143 [ $row, $this->getContext(), &$feedItem ]
145 // Hook returned a valid feed item
146 if ( $feedItem instanceof FeedItem
) {
148 // Hook was canceled and did not return a valid feed item
149 } elseif ( !$hookResult ) {
153 // Hook completed and did not return a valid feed item
154 $title = Title
::makeTitle( (int)$row->page_namespace
, $row->page_title
);
155 $user = $this->getUser();
157 if ( $title && $this->getPermissionManager()->userCan( 'read', $user, $title ) ) {
158 $date = $row->rev_timestamp
;
159 $comments = $title->getTalkPage()->getFullURL();
160 $revision = $this->revisionStore
->newRevisionFromRow( $row );
163 $title->getPrefixedText(),
164 $this->feedItemDesc( $revision ),
165 $title->getFullURL( [ 'diff' => $revision->getId() ] ),
167 $this->feedItemAuthor( $revision ),
176 * @since 1.32, takes a RevisionRecord instead of a Revision
177 * @param RevisionRecord $revision
180 protected function feedItemAuthor( RevisionRecord
$revision ) {
181 $user = $revision->getUser();
182 return $user ?
$user->getName() : '';
186 * @since 1.32, takes a RevisionRecord instead of a Revision
187 * @param RevisionRecord $revision
190 protected function feedItemDesc( RevisionRecord
$revision ) {
191 $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text();
193 $content = $revision->getContent( SlotRecord
::MAIN
);
194 } catch ( RevisionAccessException
$e ) {
198 if ( $content instanceof TextContent
) {
199 // only textual content has a "source view".
200 $html = nl2br( htmlspecialchars( $content->getText() ) );
202 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
203 // contain JS magic and generally may not be suitable for inclusion in a feed.
204 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
205 // Compare also FeedUtils::formatDiffRow.
209 $comment = $revision->getComment();
211 return '<p>' . htmlspecialchars( $this->feedItemAuthor( $revision ) ) . $msg .
212 htmlspecialchars( FeedItem
::stripComment( $comment ?
$comment->text
: '' ) ) .
213 "</p>\n<hr />\n<div>" . $html . '</div>';
216 public function getAllowedParams() {
217 $feedFormatNames = array_keys( $this->getConfig()->get( 'FeedClasses' ) );
221 ApiBase
::PARAM_DFLT
=> 'rss',
222 ApiBase
::PARAM_TYPE
=> $feedFormatNames
225 ApiBase
::PARAM_TYPE
=> 'user',
226 ApiBase
::PARAM_REQUIRED
=> true,
229 ApiBase
::PARAM_TYPE
=> 'namespace'
232 ApiBase
::PARAM_TYPE
=> 'integer'
235 ApiBase
::PARAM_TYPE
=> 'integer'
238 ApiBase
::PARAM_ISMULTI
=> true,
239 ApiBase
::PARAM_TYPE
=> array_values( ChangeTags
::listDefinedTags() ),
240 ApiBase
::PARAM_DFLT
=> '',
242 'deletedonly' => false,
245 'hideminor' => false,
247 ApiBase
::PARAM_DFLT
=> false,
251 if ( $this->getConfig()->get( 'MiserMode' ) ) {
252 $ret['showsizediff'][ApiBase
::PARAM_HELP_MSG
] = 'api-help-param-disabled-in-miser-mode';
258 protected function getExamplesMessages() {
260 'action=feedcontributions&user=Example'
261 => 'apihelp-feedcontributions-example-simple',